package org.example.config.gaud;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class ReverseGeocodingTest {

    // 封装逆地理编码的方法
    public static String reverseGeocode(double longitude, double latitude) throws Exception {

         String apiKey="53c90a235c7bda17b79fad57231b51bd";
        // 构建URL
        String urlString = "https://restapi.amap.com/v3/geocode/regeo?location=" + longitude + "," + latitude + "&key=" + apiKey;

        URL url = new URL(urlString);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.connect();

        // 读取响应
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String inputLine;
        StringBuffer content = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            content.append(inputLine);
        }
        in.close();
        connection.disconnect();
        return content.toString();
    }

    public static void main(String[] args) {
        try {
            // 经纬度坐标
           double  longitude=116.208741;
            double latitude=40.136902;

            // 调用封装的方法
            String response = reverseGeocode(longitude, latitude);
            // 打印响应结果
            System.out.println(response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

